﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;

using KaixinRobot;

namespace KaixinHouse
{
    //用户类
    public class Account
    {
        public UserConfig config = null;
        public UserIdentity identity = null;
        public House house = null;
        public Garden garden = null;

        public Account(string fn)
        {
            this.config = new UserConfig(fn);
            this.identity = this.config.master;
            this.house = new House(this);
            this.garden = new Garden(this);
        }

        //专门为Slave.ini提供构造函数
        public Account(UserIdentity uid)
        {
            this.config = UserConfig._default;
            this.identity = uid;
            this.house = new House(this);
            this.garden = new Garden(this);
        }


        public static void RunAll()
        {
            string appName = Process.GetCurrentProcess().MainModule.FileName;
            string[] filelist = Directory.GetFiles(Path.GetDirectoryName(appName), "*.ini");
            Array.Sort(filelist);

            foreach (string filename in filelist)
            {
                try
                {
                    TraceLog.PrintLn("读取账号文件: " + filename);
                    if (filename.IndexOf("Slave") >= 0)
                    {
                        //处理Slave.ini
                        UserConfig._default = new UserConfig(filename);
                        Collection<UserIdentity> slaveList = UserConfig._default.slaveList;
                        UserConfig._default.slaveList = new Collection<UserIdentity>();

                        foreach (UserIdentity uid in slaveList)
                        {
                            UserConfig._default.master = uid;
                            UserConfig._default.sendList = new Collection<int>();
                            UserConfig._default.sendList.Add(TextParser.GetInteger(uid.userIdx));

                            uid.userIdx = null;

                            Account acc = new Account(uid);
                            acc.run();
                        }
                    }
                    else
                    {
                        Account acc = new Account(filename);
                        acc.run();
                    }
                }
                catch (Exception e)
                {
                    TraceLog.Error(e.Message);
                }
            }
        }


        public int doWork()
        {
            if ((this.config.doWork == 1) && (RunTime.isANewDay()))
            {
                if (!Component.logon(this.identity)) return 0;
                RunTime.setCurEnv(this.identity.userMail, "work");
                string userID = this.identity.userIdx;

                string content = KXWeb.HouseJob.Get_JobList();
                string verifyCode = TextParser.GetVerifyCode(content);
                if (content.IndexOf("要注意身体，别累坏了") >= 0)
                {
                    //你正在做餐厅洗碗工，今天不能再干其它工作了！
                    string current_work = (new TextParser(content)).getStrBtw("你正在做", "，今天不能再干其它工作了！");
                    TraceLog.PrintLn("[{0}] is doing {1} ！", userID, current_work);
                    return -1;
                }

                //<div class="l"><input name="petid" type="radio" value="11" /> 做洗浴城领班</div>
                //<div class="l"><input name="petid" type="radio" value="43" /> 做律师</div>
                //<div class="r c9">工资：800元/天</div>
                string petid = null, name = null, tmp = null;
                string maxWkStr = null, maxWkSla = null;
                TextParser tpWk = new TextParser(content, "<input name=\"petid\" type=\"radio\" value=", "</div>");
                TextParser tpSl = new TextParser(content, "<div class=\"r c9\">", "</div>");
                while ((tmp = tpWk.getNextStrBtw()) != null)
                {
                    maxWkStr = tmp;
                    maxWkSla = tpSl.getNextStrBtw();
                }
                //"11" /> 做洗浴城领班
                petid = (new TextParser(maxWkStr)).getStrBtw("\"", "\" /> ");
                name = (new TextParser(maxWkStr)).getStrBtw("/> ", "");
                if ((petid == null) || (name == null)) return -1;

                TraceLog.PrintLn("[{0}] starts to {1}！", userID, name);
                TraceLog.PrintLn(maxWkSla);

                content = KXWeb.HouseJob.Do_Job(petid);
            }
            return 1;
        }

        public bool run()
        {
            //调用房间
            if (RunTime.needRunAgain()) return false;
            house.Action();

            //调用花园
            if (RunTime.needRunAgain()) return false;
            garden.Action();

            //用户打工
            if (RunTime.needRunAgain()) return false;
            this.doWork();

            return true;
        }

        public bool run(string exptuser, string exptcomp)
        {
            if (!String.IsNullOrEmpty(exptuser))
            {
                if (!exptuser.Equals(this.identity.userMail))
                {
                    //如果期待运行的不是当前用户，直接返回
                    return false;
                }
            }

            int runComp = 0;
            if (!String.IsNullOrEmpty(exptcomp))
            {
                if (exptcomp.Equals("work")) runComp = 1;
                if (exptcomp.Equals("garden")) runComp = 2;
                if (exptcomp.Equals("house")) runComp = 3;
            }
            if (runComp == 0) return false;

            //if (!Account.logon(this.identity)) return false;

            if (runComp == 1) this.doWork();
            if (runComp == 2) garden.Action();
            if (runComp == 3) house.Action();

            return true;
        }


    }
}
